# -*- mode: python ; coding: utf-8 -*-

import sys
import os
from pathlib import Path
from PyInstaller.utils.hooks import collect_data_files, collect_submodules

block_cipher = None

# 获取web目录的路径
web_path = Path('web').absolute()

# 获取虚拟环境路径
venv_path = os.path.dirname(os.path.dirname(sys.executable))
venv_site_packages = os.path.join(venv_path, 'Lib', 'site-packages')

# 收集所有 win32com 相关模块
try:
    win32com_modules = collect_submodules('win32com')
except ImportError:
    print("Warning: Could not collect win32com modules")
    win32com_modules = []

# 收集时区数据
tzdata_files = collect_data_files('tzdata')

a = Analysis(
    ['main.py'],
    pathex=[
        venv_site_packages,
        os.path.join(venv_site_packages, 'win32'),
        os.path.join(venv_site_packages, 'win32/lib'),
    ],
    binaries=[],
    datas=[
        (str(web_path), 'web'),
        *tzdata_files,  # 添加时区数据文件
    ],
    hiddenimports=[
        'win32print',
        'win32api',
        'win32con',
        'win32com',
        'pythoncom',
        'win32com.client',
        'win32com.client.gencache',
        'win32com.shell',
        'win32com.server',
        *win32com_modules,
        'uvicorn.logging',
        'uvicorn.protocols',
        'uvicorn.lifespan',
        'uvicorn.lifespan.on',
        'uvicorn.lifespan.off',
        'uvicorn.protocols.http',
        'uvicorn.protocols.http.auto',
        'uvicorn.protocols.websockets',
        'uvicorn.protocols.websockets.auto',
        'uvicorn.protocols.websockets.wsproto_impl',
        'uvicorn.protocols.http.h11_impl',
        'uvicorn.protocols.http.httptools_impl',
        'app.models',
        'app.database',
        'app.auth',
        'passlib.handlers.bcrypt',
        'bcrypt',
        'tzdata',
        'sqlalchemy.sql.default_comparator',
        'tzdata.zoneinfo',  # 更精确的导入
        'tzdata.zoneinfo.Asia',  # 确保包含 Asia 时区
        'importlib.resources',  # 添加 importlib.resources
    ],
    hookspath=['hooks'],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)

pyz = PYZ(
    a.pure,
    a.zipped_data,
    cipher=block_cipher
)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='timerposvc',
    debug=True,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=['python*.dll', 'pythoncom*.dll', 'pywintypes*.dll'],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
) 